/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including but not limited to the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to provide persons with access to the Software
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGE OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

<template>
  <div>
    <div class="tab-container">
      <img
        class="back-icon"
        src="../../assets/videoList/images/backIcon.png"
        alt=""
        @click="goBack"
      />
      <div class="tab-bar">
        <div class="tab-scroll">
          <div
            v-for="(tab, index) in tabs"
            :key="tab.id"
            class="tab-button"
            :class="{ 'active-tab': activeTab === index }"
            @click="setActiveTab(index)"
          >
            <span
              class="tab-text"
              :class="{ 'active-tab-text': activeTab === index }"
            >
              {{ tab.title }}
            </span>
          </div>
        </div>
      </div>
      <div class="tab-more">
        <div class="tab-search" v-show="isLandscape">
          <img
            class="tab-search-img"
            src="../../assets/videoList/images/lg-search.png"
            alt=""
          />
          <input class="tab-search-input" placeholder="搜索..." />
        </div>
        <img
          class="tab-more-img"
          v-show="isLandscape"
          src="../../assets/videoList/images/history.png"
          alt=""
        />
        <img
          class="tab-more-img"
          src="../../assets/videoList/images/rightMore.png"
          alt=""
        />
      </div>
    </div>
    <div
      class="tab-more"
      v-show="!isLandscape"
      :style="{ marginBottom: isLandscape ? '0' : '8px' }"
    >
      <div class="tab-search">
        <img
          class="tab-search-img"
          src="../../assets/videoList/images/lg-search.png"
          alt=""
        />
        <input class="tab-search-input" placeholder="搜索..." />
      </div>
      <img
        class="tab-more-img"
        src="../../assets/videoList/images/history.png"
        alt=""
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, onBeforeUnmount } from "vue";
import { tabs } from "./mockData";
import { useRouter } from "vue-router";
import { BreakpointManager } from "@hadss/web_adaptive_layout_ui";

const breakpointManager = new BreakpointManager();
const activeTab = ref(0);
const setActiveTab = (index: number) => {
  activeTab.value = index;
};
const router = useRouter();
const goBack = () => {
  router.go(-1);
};
const isLandscape = ref(false);

const unsubscribe = breakpointManager.subscribeToBreakpoint(() => {
  isLandscape.value = breakpointManager.useBreakpointValue({
    sm: false,
    md: true,
  });
});

onBeforeUnmount(() => {
  if (unsubscribe) {
    unsubscribe();
  }
});
</script>

<style scoped>
.tab-container {
  display: flex;
  width: 100%;
  height: 56px;
  align-items: center;
}

.tab-bar {
  display: flex;
  flex-direction: row;
  flex: 1;
  overflow-x: auto;
  -ms-overflow-style: none;
  scrollbar-width: none;
}

.tab-bar::-webkit-scrollbar {
  display: none;
}

.back-icon {
  width: 40px;
  height: 40px;
  padding-right: 10px;
}

.tab-scroll {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.tab-more {
  display: flex;
  align-items: center;
  padding-left: 8px;
  margin-right: 0;
}

.tab-more-img {
  width: 40px;
  height: 40px;
  padding-right: 8px;
}

.tab-more-img:last-child {
  padding-right: 0;
}

.tab-search {
  width: 100%;
  min-width: 138px;
  height: 40px;
  line-height: 40px;
  border-radius: 20px;
  background-color: rgba(241, 243, 245, 0.8);
  padding-right: 12px;
  padding-left: 12px;
  margin-right: 12px;
  text-align: start;
}

.tab-search-img {
  width: 12px;
  height: 12px;
}

.tab-search-input {
  all: unset;
  padding-left: 12px;
}

.tab-moreMobile {
  padding: 8px 0 8px 0;
}

.tab-button {
  width: 60px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 40px;
  margin-right: 8px;
  background-color: rgba(0, 0, 0, 0.05);
  cursor: pointer;
  transition: background-color 0.3s;
}

.tab-button.active-tab {
  background-color: #0a59f7;
}

.tab-text {
  font-size: 16px;
  line-height: 40px;
  color: #000;
  text-align: center;
}

.tab-text.active-tab-text {
  color: #fff;
}

.tab-more-tablet {
  width: 50%;
}
</style>